package com.suanfa.demo;

public class Linkedlist {
    private Node headNode = null;
    private Node tail = null;

    //set和get方法
    public Node getHeadNode() {
        return headNode;
    }

    public void setHeadNode(Node headNode) {
        this.headNode = headNode;
    }

    public Node getTail() {
        return tail;
    }

    public void setTail(Node tail) {
        this.tail = tail;
    }

    //构造方法
    public Linkedlist(Node headNode, Node tail) {
        this.headNode = headNode;
        this.tail = tail;
    }

    public Linkedlist() {
    }

    //一般方法
    public void printItems(){
        //遍历所有的Node
        Node p = this.headNode;
        while(p != null){
            System.out.println(p.getValue());
            p = p.getNextNode();
        }
    }

    public void removeNode(String value){
        //删除指定的node
        Node p = this.headNode;
        Node frontNode = null;
            while (p != null){
                if (p.getValue() == value) {
                    if (p == this.headNode) {
                        Node nextNode = p.getNextNode();
                        p = null;
                        this.headNode = nextNode;
                        System.out.println("finish");
                        break;
                    }
                    else {
                        frontNode.setNextNode(p.getNextNode());
                        p = null;
                        System.out.println("finish");
                        break;
                    }
                }
                else{
                    frontNode = p;
                    p = p.getNextNode();
                }
            }
        }

    public void removeNode(int item){
        //删除指定item所指定的node
        Node p = this.headNode;
        for (int i=0; i<item; i++){
            p = p.getNextNode();
            if (p == null){
                System.out.println("error");
            }
        }
        removeNode(p.getValue());
    }

    public void appendFront(String value){
        //将node插入linkedlist的头部
        Node node = new Node(value);
        if (headNode != null){
            Node p = headNode;
            node.setNextNode(p);
            node.setPrevNode(null);
            this.headNode = node;
        }else{
            node.setPrevNode(null);
            this.headNode = node;
        }
        if (size() == 1){
            node.setNextNode(null);
            tail = node;
        }

    }

    public void appendNode(String value,int item){
        //插入node在指定的位置
        if (size() == 0){
            appendFront(value);
        } else if (item == 0 ){
            appendFront(value);
        }else {
            Node node = new Node(value);
            Node p = this.headNode;
            for (int i = 0; i < item - 1 ; i++) {
                p = p.getNextNode();
                if (p == null){
                    appendEnd(value);
                    break;
                }
            }
            node.setNextNode(p.getNextNode());
            node.setPrevNode(p);
            p.setNextNode(node);

        }
    }

    public void appendEnd(String value){
        //插入node到linkedlist尾部
        if (size() == 0){
            appendFront(value);
        }else {
            Node p = headNode;
            while (p.getNextNode() != null) {
                p = p.getNextNode();
            }
            Node node = new Node(value);
            p.setNextNode(node);
            node.setNextNode(null);
            node.setPrevNode(p);
            tail = node;
        }
    }

    public String findNode(int item){
        //返回指定node的位置
        Node p = this.headNode;
        for (int i=0; i<item; i++){
            p = p.getNextNode();
            if (p == null){
                return "error";
            }
        }
        return p.getValue();
    }

    public String findItem(String value){
        //返回指定位置node的value值
        Node p = this.headNode;
        int item = 0;
        while (p != null){
            if (p.getValue() == value){
                return String.valueOf(item);
            }
            p = p.getNextNode();
            item ++;
        }
        return "error";
    }

    public boolean findItem_bool(String value){
        //返回指定位置node的value值
        Node p = this.headNode;
        int item = 0;
        while (p != null){
            if (p.getValue() == value){
                return true;
            }
            p = p.getNextNode();
            item ++;
        }
        return false;
    }

    public int size(){
        //返回linklist的大小
        Node p = headNode;
        int i = 0;
        while (p != null){
            p = p.getNextNode();
            i ++;
        }
        return i;
    }


    

}




